import { lazy } from "react";
import { Navigate } from "react-router-dom";
import Login from "../views/Login";
import BasicLayout from "../layouts/BasicLayout";
import Permission from "../views/Permission";
import TestPermission from "../views/TestPermission";

// 路由懒加载
const Home = lazy(() => import("../views/Home"));
const User = lazy(() => import("../views/User"));
const Profile = lazy(() => import("../views/Profile"));
const Settings = lazy(() => import("../views/Settings"));
const Role = lazy(() => import("../views/Role"));
const RolePermission = lazy(() => import("../views/RolePermission"));
const Leave = lazy(() => import("../views/Leave"));
const AIChat = lazy(() => import("../views/AIChat"));
const Article = lazy(() => import("../views/ArticleManage"));

// 路由元信息接口
export interface RouteMeta {
  title: string;
  requiresAuth: boolean;
  icon?: string;
  permission?: string;
}

// 扩展路由对象类型
export interface AppRouteObject {
  path: string;
  element: React.ReactNode;
  meta?: RouteMeta;
  children?: AppRouteObject[];
}

export const routes: AppRouteObject[] = [
  {
    path: "/",
    element: <BasicLayout />,
    meta: {
      title: "首页",
      requiresAuth: true,
    },
    children: [
      {
        path: "",
        element: <Home />,
        meta: {
          title: "首页",
          requiresAuth: true,
        },
      },
      {
        path: "user",
        element: <User />,
        meta: {
          title: "用户管理",
          requiresAuth: true,
          permission: "view:user",
        },
      },
      {
        path: "role",
        element: <Role />,
        meta: {
          title: "角色管理",
          requiresAuth: true,
          permission: "view:role",
        },
      },
      {
        path: "role-permission",
        element: <RolePermission />,
        meta: {
          title: "角色权限配置",
          requiresAuth: true,
          permission: "assign:permission",
        },
      },
      {
        path: "permission",
        element: <Permission />,
        meta: {
          title: "权限配置",
          requiresAuth: true,
          permission: "view:permission",
        },
      },
      {
        path: "profile",
        element: <Profile />,
        meta: {
          title: "个人信息",
          requiresAuth: true,
        },
      },
      {
        path: "settings",
        element: <Settings />,
        meta: {
          title: "系统设置",
          requiresAuth: true,
          permission: "view:settings",
        },
      },
      {
        path: "test-permission",
        element: <TestPermission />,
        meta: {
          title: "测试权限",
          requiresAuth: true,
        },
      },
      {
        path: "leave",
        element: <Leave />,
        meta: {
          title: "请假流程",
          requiresAuth: true,
          permission: undefined,
        },
      },
      {
        path: "ai-chat",
        element: <AIChat />,
        meta: {
          title: "AI 对话助手",
          requiresAuth: true,
          permission: undefined,
        },
      },
      {
        path: "article-manage",
        element: <Article />,
        meta: {
          title: "文章管理",
          requiresAuth: true,
          permission: undefined,
        },
      },
    ],
  },
  {
    path: "/login",
    element: <Login />,
    meta: {
      title: "登录",
      requiresAuth: false,
    },
  },
  {
    path: "*",
    element: <Navigate to="/" replace />,
  },
];

export default routes;
